package cn.jeeweb.modules.lizhu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import cn.jeeweb.core.common.service.impl.CommonServiceImpl;
import cn.jeeweb.core.query.wrapper.EntityWrapper;
import cn.jeeweb.core.utils.ServletUtils;
import cn.jeeweb.core.utils.StringUtils;
import cn.jeeweb.modules.lizhu.entity.Archive;
import cn.jeeweb.modules.lizhu.entity.ArchiveInfo;
import cn.jeeweb.modules.lizhu.mapper.ArchiveMapper;
import cn.jeeweb.modules.lizhu.service.IArchiveInfoService;
import cn.jeeweb.modules.lizhu.service.IArchiveService;

/**   
 * @Title: 经典案例
 * @Description: 经典案例
 * @author yaofanhui
 * @date 2018-01-27 16:59:47
 * @version V1.0   
 *
 */
@Transactional
@Service("archiveService")
public class ArchiveServiceImpl  extends CommonServiceImpl<ArchiveMapper,Archive> implements  IArchiveService {
	@Autowired
	private IArchiveInfoService archiveInfoService;
	
	@Override
	public boolean insert(Archive archive) {
		// 保存主表
		super.insert(archive);
		// 保存tm_archive_info
		String tmArchiveInfoListJson = StringEscapeUtils
				.unescapeHtml4(ServletUtils.getRequest().getParameter("tmArchiveInfoListJson"));
		List<ArchiveInfo> tmArchiveInfoList = JSONObject.parseArray(tmArchiveInfoListJson, ArchiveInfo.class);
		for (ArchiveInfo archiveInfo : tmArchiveInfoList) {
			// 保存字段列表
			archiveInfo.setPid(archive.getId());
			archiveInfoService.insert(archiveInfo);
		}
		return true;
	}
	
	@Override
	public boolean insertOrUpdate(Archive archive) {
		try {
			// 获得以前的数据
			List<ArchiveInfo> oldArchiveInfoList = archiveInfoService.selectList(new EntityWrapper<ArchiveInfo>(ArchiveInfo.class).eq("pid.id",archive.getId()));
			// 字段
			String archiveInfoListJson = StringEscapeUtils
					.unescapeHtml4(ServletUtils.getRequest().getParameter("tmArchiveInfoListJson"));
			List<ArchiveInfo> archiveInfoList = JSONObject.parseArray(archiveInfoListJson,
					ArchiveInfo.class);
			// 更新主表
			super.insertOrUpdate(archive);
			archiveInfoList = JSONObject.parseArray(archiveInfoListJson, ArchiveInfo.class);
			List<String> newsArchiveInfoIdList = new ArrayList<String>();
			// 保存或更新数据
			for (ArchiveInfo archiveInfo : archiveInfoList) {
				// 设置不变更的字段
				if (StringUtils.isEmpty(archiveInfo.getId())) {
					// 保存字段列表
					archiveInfo.setPid(archive.getId());
					archiveInfoService.insert(archiveInfo);
				} else {
					archiveInfoService.insertOrUpdate(archiveInfo);
				}
				newsArchiveInfoIdList.add(archiveInfo.getId());
			}

			// 删除老数据
			for (ArchiveInfo archiveInfo : oldArchiveInfoList) {
				String archiveInfoId = archiveInfo.getId();
				if (!newsArchiveInfoIdList.contains(archiveInfoId)) {
					archiveInfoService.deleteById(archiveInfoId);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}
	
	
	
}
